package cn.flying.cloud.system.common.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.core.auth.details.UserDetail;
import cn.flying.cloud.system.log.entity.SysLoginLog;
import cn.flying.cloud.system.log.entity.SysOperLog;
import cn.flying.cloud.system.log.service.SysLoginLogService;
import cn.flying.cloud.system.log.service.SysOperLogService;
import cn.flying.cloud.system.user.entity.SysUserInfo;
import cn.flying.cloud.system.user.service.SysUserInfoService;


/**
 * @description: 系统用户服务
 * @author: admin
 * @date: 2023年05月24日 13:12
 * @version: 1.0
 */
@Service
public class UserService {

    @Resource
    private SysUserInfoService sysUserInfoService;
    @Resource
    private SysLoginLogService sysLoginLogService;
    @Resource
    private SysOperLogService sysOperLogService;


    /**
     * 通过用户名查询用户
     *
     * @param username
     * @return
     */
    public UserDetail findUserByUsername(String username) {
        SysUserInfo userInfo = new SysUserInfo();
        userInfo.setUsername(username);
        userInfo = sysUserInfoService.getOneObj(userInfo);
        UserDetail userDetail = new UserDetail();
        Ft.BeanCopier.copy(userInfo, userDetail);
        return userDetail;
    }

    /**
     * 更新用户的最后登录时间
     *
     * @param username
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateUserLoginTimeByUsername(String username) {
//        sysUserInfoService.updateUserLoginTimeByUsername(username);
    }

    /**
     * 保存登录日志
     *
     * @param log
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertLoginLog(SysLoginLog log) {
        sysLoginLogService.createObj(log);
    }

    /**
     * 保存操作日志
     *
     * @param log
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertOperLog(SysOperLog log) {
        sysOperLogService.createObj(log);
    }

}
